package win.smartown.android.app.scrollPager.demo

import android.graphics.Color
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import win.smartown.android.library.scrollpager.ScrollPager
import win.smartown.android.library.scrollpager.impl.ScrollableListView
import win.smartown.android.library.scrollpager.interfaces.Adapter
import win.smartown.android.library.scrollpager.interfaces.Scrollable

class MainActivity : AppCompatActivity() {

    private val scrollPager by lazy { findViewById(R.id.scrollPager) as ScrollPager }
    private val menuListView by lazy { findViewById(R.id.list) as ListView }
    private val grid1 by lazy {
        Pager.Grid("手机", arrayListOf(
                Pager.Grid.Item("小米MIX2", R.mipmap.mimix2),
                Pager.Grid.Item("小米Note3", R.mipmap.minote3),
                Pager.Grid.Item("小米6", R.mipmap.mi6))
        )
    }
    private val grid2 by lazy {
        Pager.Grid("手机", arrayListOf(
                Pager.Grid.Item("小米MIX2", R.mipmap.mimix2),
                Pager.Grid.Item("小米Note3", R.mipmap.minote3),
                Pager.Grid.Item("小米6", R.mipmap.mi6),
                Pager.Grid.Item("小米5X", R.mipmap.mi5x))
        )
    }
    private val grid3 by lazy {
        Pager.Grid("手机", arrayListOf(
                Pager.Grid.Item("小米MIX2", R.mipmap.mimix2),
                Pager.Grid.Item("小米Note3", R.mipmap.minote3),
                Pager.Grid.Item("小米6", R.mipmap.mi6),
                Pager.Grid.Item("小米5X", R.mipmap.mi5x),
                Pager.Grid.Item("小米Max2", R.mipmap.mimax2))
        )
    }
    private val grid4 by lazy {
        Pager.Grid("手机", arrayListOf(
                Pager.Grid.Item("小米MIX2", R.mipmap.mimix2),
                Pager.Grid.Item("小米Note3", R.mipmap.minote3),
                Pager.Grid.Item("小米6", R.mipmap.mi6),
                Pager.Grid.Item("小米5X", R.mipmap.mi5x),
                Pager.Grid.Item("小米Max2", R.mipmap.mimax2),
                Pager.Grid.Item("红米Note 5A", R.mipmap.redminote5a)
        ))
    }

    private val pagers by lazy {
        arrayListOf(Pager(R.mipmap.mibanner, "推荐", arrayListOf(grid1, grid2, grid3, grid4)),
                Pager(R.mipmap.mibanner, "手机", arrayListOf(grid1, grid2, grid3, grid4)),
                Pager(R.mipmap.mibanner, "电视", arrayListOf(grid1, grid2, grid3, grid4)),
                Pager(R.mipmap.mibanner, "电脑", arrayListOf(grid1, grid2, grid3, grid4)))
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        scrollPager.setAdapter(PagerAdapter())
        val menuAdapter = MenuAdapter()
        menuListView.adapter = menuAdapter
        menuListView.setOnItemClickListener { adapterView, view, i, l -> scrollPager.scrollToChildAt(i) }
        scrollPager.setOnPagerChangedListener { scrollPager, position -> menuAdapter.notifyDataSetChanged() }
    }

    inner class PagerAdapter : Adapter {
        override fun getPagerCount(): Int {
            return pagers.size
        }

        override fun getPager(position: Int): Scrollable {
            val contentView = LayoutInflater.from(this@MainActivity).inflate(R.layout.layout_item_pager, null) as ScrollableListView
            contentView.adapter = ListAdapter(position)
            return contentView
        }

    }

    inner class ListAdapter(val position: Int) : BaseAdapter() {
        override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
            if (p0 == 0) {
                val imageView = ImageView(this@MainActivity)
                imageView.setImageResource(pagers[position].image)
                return imageView
            } else {
                val itemView = LayoutInflater.from(this@MainActivity).inflate(R.layout.layout_item_grid, null)
                itemView.findViewById<TextView>(R.id.title).text = pagers[position].grid[p0 - 1].title
                itemView.findViewById<GridView>(R.id.items).adapter = GridAdapter(pagers[position].grid[p0 - 1].items)
                return itemView
            }
        }

        override fun getItem(p0: Int): Any {
            return ""
        }

        override fun getItemId(p0: Int): Long {
            return p0.toLong()
        }

        override fun getCount(): Int {
            return pagers[position].grid.size + 1
        }
    }

    inner class GridAdapter(val items: ArrayList<Pager.Grid.Item>) : BaseAdapter() {

        override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
            val itemView = LayoutInflater.from(this@MainActivity).inflate(R.layout.layout_item_grid_item, null)
            itemView.findViewById<ImageView>(R.id.item_image).setImageResource(items[p0].image)
            itemView.findViewById<TextView>(R.id.item_title).text = items[p0].title
            return itemView
        }

        override fun getItem(p0: Int): Any {
            return ""
        }

        override fun getItemId(p0: Int): Long {
            return p0.toLong()
        }

        override fun getCount(): Int {
            return items.size
        }

    }

    inner class MenuAdapter() : BaseAdapter() {
        override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
            val itemView = LayoutInflater.from(this@MainActivity).inflate(R.layout.layout_item_list, null)
            itemView.findViewById<TextView>(R.id.list_title).text = pagers[p0].title
            if (p0 == scrollPager.currentChildIndex) {
                itemView.findViewById<View>(R.id.list_selected).visibility = View.VISIBLE
                itemView.findViewById<TextView>(R.id.list_title).setTextColor(Color.parseColor("#ff6b00"))
            } else {
                itemView.findViewById<View>(R.id.list_selected).visibility = View.GONE
                itemView.findViewById<TextView>(R.id.list_title).setTextColor(Color.parseColor("#454545"))
            }
            return itemView
        }

        override fun getItem(p0: Int): Any {
            return ""
        }

        override fun getItemId(p0: Int): Long {
            return p0.toLong()
        }

        override fun getCount(): Int {
            return pagers.size
        }

    }

}
